/*Outreach splicing game*/

// Constants - their value won't change.
// They're used here to set pin numbers:
?????add a constant variable called buttonPin and set it's value to 0, because the button will be put in pin 0?????     // the number of the pushbutton pin
?????to add a consant variable, put the keyword "const" before the variable type "int"???

// Variables - their values will change:
int gameCounter = 0;           // variable for checking how many times the button has been pressed
int gSpeed = 800;       // initial game speed - time in milliseconds each led is turned on
int prevButtonState = LOW;    // Variable to ensure two button presses are NOT recorded if the button is simply held down
int currentButtonState = LOW; // Variable to ensure two button presses are NOT recorded if the button is simply held down
int firstPress = 0;       // Variable to record which LED was active when the button was pressed the first time
int secondPress = 0;      // Variable to record which LED was active when the button was pressed the second time
bool buttonPressed = false;   // Variable to tell you when to end for loops

// the setup function runs once when you press reset or power the board
void setup() {
  // Initialize all digital pins as output pins.
  ?????Setup all the LED pins - pins 1 to 13 - using a for loop. This should be the same as in exercise 3?????
  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

  
}

//STUDENTS - IGNORE THIS FOR NOW
//used in loop for logic
void levelUp(int press1, int press2){
      if (press1 == 4 && press2 == 9){
        //Level PASSED!! Level up!
        //update gSpeed value
        gSpeed = gSpeed/2;


      }
      else{
        //FAILED TO SPLICE. Speed stays the same
      }
      //Flash winning values
        for(int j = 0; j<6; j = j + 1){
          digitalWrite(press1, HIGH);
          delay(500); //0.5 seconds on
          digitalWrite(press1, LOW);
          digitalWrite(press2, HIGH);
          delay(500); //0.5 seconds on 
          digitalWrite(press2, LOW);  }
}

//STUDENTS - IGNORE THIS FOR NOW
//handle what happens when button is pressed
bool gameLogic(int i){

    bool output = false;
    
    //get current push button state
    currentButtonState = ! digitalRead(buttonPin);

    if(currentButtonState == HIGH){//button is pressed
      //check to see if button is just being held down
      if(prevButtonState == HIGH){
        //pass. Button is being held down. 'Previous button state' remains high
        }
      else{
        
        //New button press
        //increment game counter
        gameCounter = gameCounter + 1;

        //test to see if time to end game
        if(gameCounter == 2){
          //end game
          //set game counter to 0
          gameCounter = 0;
          //set second press position identifier
          secondPress = i;

          //turn off most recent led
          digitalWrite(i, LOW);
          
          //display result and continue/level up
          if(firstPress<secondPress) levelUp(firstPress, secondPress);
          else levelUp(secondPress, firstPress);

          //reset first and second press values
          firstPress = 0;
          secondPress = 0;

          //set previous button press to LOW
          prevButtonState = LOW;
          currentButtonState = LOW;
          
          //break out of the for loop to restart the game for the next round
          output = true;
      
          //Turn off current LED
          digitalWrite(i, LOW);
        }
        else{
      //Button HAS been pressed, but this is the first time this game it has been pressed, so continue the game.
          // first time button pressed. set first press position identifier
          firstPress = i;
          
          //set previous button state to HIGH
          prevButtonState = HIGH;
        }
        
      }
    }
    else{//button not pressed. Set previous button press val to LOW
      prevButtonState = LOW;
    }

    return output;
}


// the loop function runs over and over again forever
void loop() {

  /*
  STUDENTS- READ THIS FIRST!!!:
  In this game, we want the light to move from one LED to the next.
  Once the last LED is reached, we want the light to move back along the row of LEDs.
  We will therefore need two for loops - one will count up from 1 to 13 and light up each of the LEDs
  in pins 1 to 13 in turn. The other will count down 
  */
  
/*=============FIRST=FOR=LOOP=======Increasing-pin-value======================================*/
  for(int i = 2; i<14; i = i + 1){
    /* turn the LED on (HIGH is the voltage level) */
    ?????Turn the current LED on?????
  
    // wait for designated time
  /*
  The reason we use a for loop here is so the program will check the button every millisecond.
  At first you may think to just have a "delay(gSpeed)" command to make the arduino wait for a
  specific time before it turns the next LED on. The problem with this is that if you press the
  button while the arduino is running the "delay" command, it won't do anything because the 
  arduino isn't checking to see if the button has been pressed or not. The arduino will just sit 
  there and do nothing - it won't detect any new input until it has finished executing the delay command. 
  In every loop of the for loop, the arduino will wait ("delay") for one millisecond, check to see 
  if the button has been pressed, and then start the loop again if the button has NOT been pressed.
  */
    for(int k = 0; k<???How many times should this for loop be run???; k = k + 1){
    //wait one millisecond
      delay(1);
    
      //perform game logic (check to see if the button is pressed)
      buttonPressed = gameLogic(i);
    
    //if the button has been pressed, exit the for loop.
      if (buttonPressed) break;
    }
  
  //if the button has been pressed, exit the second for loop
    if(buttonPressed){
      buttonPressed = false;
      break;  
    }
    //If the arduino gets to this point in the code then the button has NOT yet been pressed.
    //We therefore have to continue with the game and turn the next LED on.
    //Before we turn the next LED on, we should turn our current LED off (the arduino doensn't
    //have enough power to supply both LEDs - they would both be very dim if we tried this).
    //Therefore, turn the curren LED off:
    ?????Turn the current led off?????    // turn the LED off by making the voltage LOW
  }

  
/*=============SECOND=FOR=LOOP=======Decreasing-pin-value========================================*/
/*=============SAME=CODE=AS=ABOVE=EXCEPT=THE=FIRST=FOR=LOOP======================================*/
  for(int i = 12; i>0; i = i - 1){
    /* turn the LED on (HIGH is the voltage level) */
    ?????Turn the current LED on?????

      // wait for designated time              
      for(int k = 0; k<???Same as above???; k = k + 1){
        delay(1);
        //perform game logic
        buttonPressed = gameLogic(i);
        if (buttonPressed) break;
      }
      if(buttonPressed){
        buttonPressed = false;
        break;  
      }
    
      ?????Turn the current LED off?????    // turn the LED off by making the voltage LOW
  }
  
}

/*===EXTRA=CODE================================================================================*/

/*===MORE=ADVANCED=GAME=====VARIABLE=SPEED=LEDS================================================*/

    /*if (i == 4 || i == 9) delay(gSpeed/3);
    else if (i == 3 || i == 5 || i == 8 || i == 10) delay(gSpeed/2);
    else delay(gSpeed);  */  

/*===SERIAL===================================================================================*/  
  
  //If we need to use the serial
  //Begin serial
  //Serial.begin(9600);
  //Serial.println("this will be printed to the serial"))